<?php

class CinemaForm extends BaseForm {
  const FORM_NAME           = 'frm';
  
  const FIELD_MERCHANT_ID   = 'merchant_id';
  const FIELD_NAME          = 'cinema_name';
  const FIELD_NUM_SCREENS   = 'num_screens';
  const FIELD_STATE_ID      = 'state_id';
  const FIELD_STREET_NUMBER = 'street_number';
  const FIELD_STREET_NAME   = 'street_name';
  const FIELD_SUBURB        = 'suburb';
  const FIELD_POSTAL_CODE   = 'postal_code';
  const FIELD_LOGO_FILE     = 'logo_file';
  const FIELD_STATUS        = 'status';

  const SORT_NAME           = 'NAME';
  const SORT_CREATED_AT     = 'CREATED_AT desc';

  public function configure() {
    $arrStatesList    = STATETable::getInstance()->findAllArray(true);
    $arrStatusList    = StringUtil::getStatusArray();
    
    $arrNumScreens = array();
    for ($i=1; $i <= 20; $i++) {
      $arrNumScreens[$i] = $i;
    }

    $widget = array(
            self::FIELD_MERCHANT_ID => new sfWidgetFormInputHidden(array(), array()),
            self::FIELD_NAME        => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_NUM_SCREENS => new sfWidgetFormSelect(array('choices' => $arrNumScreens)),
            self::FIELD_STATE_ID    => new sfWidgetFormSelect(array('choices' => $arrStatesList)),
            self::FIELD_STREET_NUMBER  => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_STREET_NAME=> new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_SUBURB     => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_POSTAL_CODE=> new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_LOGO_FILE  => new sfWidgetFormInputFile(array(), array()),
            self::FIELD_STATUS     => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
            self::FIELD_MERCHANT_ID => 'Merchant<req />',
            self::FIELD_NAME        => 'Name<req />',
            self::FIELD_NUM_SCREENS => 'Number of Screens<req />',
            self::FIELD_STATE_ID    => 'State<req />',
            self::FIELD_STREET_NUMBER => 'Street Number<req />',
            self::FIELD_STREET_NAME => 'Street Name<req />',
            self::FIELD_SUBURB      => 'Suburb<req />',
            self::FIELD_POSTAL_CODE => 'Postal Code<req />',
            self::FIELD_LOGO_FILE   => 'Logo',
            self::FIELD_STATUS      => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_MERCHANT_ID => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_MERCHANT_ID])))),
            self::FIELD_NAME        => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_NAME])))),
            self::FIELD_NUM_SCREENS => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_NUM_SCREENS])))),
            self::FIELD_STATE_ID    => new sfValidatorNumber(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATE_ID])))),
            self::FIELD_STREET_NUMBER  => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STREET_NUMBER])))),
            self::FIELD_STREET_NAME  => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STREET_NAME])))),
            self::FIELD_SUBURB      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_SUBURB])))),
            self::FIELD_POSTAL_CODE  => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_POSTAL_CODE])))),
            self::FIELD_LOGO_FILE  => new sfValidatorFile(array('required' => false,'max_size' => (300*1024), 'mime_types' => array('image/jpg', 'image/jpeg')), array('max_size' => 'Logo: maximum file size allowed is 300kb.', 'mime_types' => 'Logo: only .jpg files are allowed.')),
            self::FIELD_STATUS      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );
    
    $this->setValidators($validators);
    //$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(CINEMA $cinema) {
    $this->setDefault(self::FIELD_MERCHANT_ID, $cinema->getMerchantId());
    $this->setDefault(self::FIELD_NAME, $cinema->getName());
    $this->setDefault(self::FIELD_NUM_SCREENS, $cinema->getNumScreens());
    $this->setDefault(self::FIELD_STATE_ID, $cinema->getStateId());
    $this->setDefault(self::FIELD_STREET_NUMBER , $cinema->getStreetNumber());
    $this->setDefault(self::FIELD_STREET_NAME, $cinema->getStreetName());
    $this->setDefault(self::FIELD_SUBURB, $cinema->getSuburb());
    $this->setDefault(self::FIELD_POSTAL_CODE, $cinema->getPostalCode());
    $this->setDefault(self::FIELD_STATUS, $cinema->getStatus());
  }
}
?>